package RobotBattleArena;

import java.util.Random;

/**
 * Created by Maxim on 15.02.2015.
 */
abstract class AbstractRobot implements Target{
    ////////// The variable declaration area \\\\\\\\\\

    protected String name;    // name of current robot
    private int health;     // amount of health, if health < 0 - robot destroyed
    private int power;      // amount of damage robot can cause
    private int defense;    // value which decreases damage, when other robot attack

    ////////// Class methods \\\\\\\\\\

    // This method describes how health reduced
    public void reduceHealth(int damageAmount){

        int currentHealth = getHealth();
        int robotDefense = getDefense();

        int damage = damageAmount > robotDefense ? damageAmount - robotDefense : 1;

        setHealth(currentHealth - damage);

        System.out.println(getName() + " recieved " + damage + " damage! Health remained: " + getHealth());

    }

    // Describes how one robot attack another
    public void attack(Target hostileRobot){

        Boolean attackAvoided;  // true - hostile robot evade attack and take no damage
        int attackRate;         // displays the probability of a successful attack

        System.out.print(getName() + " attacks " + hostileRobot.getName() + " | ");

        Random generator = new Random();

        attackRate = generator.nextInt(100);

        attackAvoided = hostileRobot.avoidAttack(attackRate);

        if (!attackAvoided){
            hostileRobot.reduceHealth(getPower());
        } else {
            System.out.println(hostileRobot.getName() + " evade attack!");
        }
    }

    /*
   // When attacked, robot can evade.
   // Returns:
   //  - true - evade was successful;
   //  - false - evasion failed.
   */
    abstract public Boolean avoidAttack(int attackRate);

    // This method override standard method
    public String toString(){
        return getName() + " (Health: " + getHealth() + "; "
                + "Power: " + getPower() + "; "
                + "Defense: " + getDefense() + ")";
    }

    ////////// Area of getters and setters \\\\\\\\\\

    // This method return robot name
    public String getName() {
        return name;
    }

    // This method set robot name
    public void setName(String name) {
        this.name = name;
    }

    // This method return robot health
    public int getHealth() {
        return health;
    }

    // This method set robot health
    public void setHealth(int health) {
        this.health = health;
    }

    // This method return robot power
    public int getPower() {
        return power;
    }

    // This method set robot power
    public void setPower(int power) {
        this.power = power;
    }

    // This method return robot defense
    public int getDefense() {
        return defense;
    }

    // This method set robot defense
    public void setDefense(int defense) {
        this.defense = defense;
    }

}
